package boardgamer.yavalath.client.replay;

import boardgamer.common.client.PeriodicTimer;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * @author garysoed
 */
public class ReplayPlayer extends PeriodicTimer {
  
  private static final int PERIOD_TIME = 3000;

  private final NextTimeHandler handler;
  private boolean isPlaying;

  protected ReplayPlayer(NextTimeHandler handler) {
    super(PERIOD_TIME);
    this.handler = checkNotNull(handler, "handler");
    this.isPlaying = false;
  }
  
  public boolean isPlaying() {
    return isPlaying;
  }
  
  public void setPlaying(boolean isPlaying) {
    this.isPlaying = isPlaying;
    if (isPlaying) {
      synchronized (millis) {
        millis.set(PERIOD_TIME);
      }
    }
  }

  @Override
  protected void onPeriodic() {
    if (isPlaying) {
      handler.handleNextTime();
    }
  }

  public interface NextTimeHandler {
    void handleNextTime();
  }
}
